<template>
  <div class="video-box" :class="{ 'show-head': showHead && showPlayer }">
    <h6 class="video-head" v-if="showHead && showPlayer">
      <p class="video-custom-title">{{ videoTitle }}</p>
      <span class="video-close" @click="closeVideo(true)"></span>
    </h6>
    <div class="video-player-container" v-if="showPlayer">
      <div class="video-player" ref="container" v-if="isLive()"></div>
      <div class="video-player" ref="video" v-else>
        <video autoplay controls :src="videoUrl" />
      </div>
    </div>
    <div class="icon-play-box" v-if="!showPlayer">
      <span class="icon-play"></span>
      <p class="icon-text">视频播放器<br /></p>
    </div>
  </div>
</template>

<script setup name="videoEasyPlayer">
export default {
  props: {
    videoTitle: {
      type: String,
      default: "",
    },
    videoUrl: {
      type: String,
      default: "",
    },
    index: {
      type: Number,
      default: 0,
    },
    showHead: {
      type: Boolean,
      default: false,
    },
    id: {
      type: String,
      default: "",
    },
    autoplay: {
      type: Boolean,
      default: true,
    },
  },
  data() {
    return {
      showPlayer: false,
      easyPlayer: null,
      container: null,
      timer: null,
    };
  },
  watch: {
    videoUrl: {
      handler(val) {
        let that = this;
        if (val) {
            this.showPlayer = true;
          if (this.easyPlayer) {
            this.easyPlayer.pause();
            setTimeout(() => {
              that.playVideo();
            }, 500);
          } else {
            let that = this;
            this.$nextTick(() => {
              that.initPlayer();
            });
          }
        }else if (this.easyPlayer) {
          this.closeVideo(false);
        }
      },
      deep: true,
      immediate: true,
    },
  },
  unmounted() {
    this.closeVideo(true);
  },
  methods: {
    initPlayer() {
      if (this.easyPlayer) {
        this.easyPlayer.destroy();
        this.easyPlayer = null;
      }
      if (this.isLive()) {
        this.easyPlayer = new EasyPlayerPro({
          container: this.$refs["container"],
          decoder: "/easyPlayer/decoder-pro.js",
          videoBuffer: 0.2, // 缓存时长
          isResize: false,
          text: "",
          loadingText: "加载中",
          // debug: true,
          // debugLevel: 'debug',
          useMSE: true,
          useSIMD: false,
          useWCS: true,
          isMulti: true,
          hasAudio: true,
          showBandwidth: false, // 显示网速
          showPerformance: false,
          operateBtns: {
            fullscreen: true,
            screenshot: true,
            play: true,
            audio: true,
            record: false,
            quality: true,
            performance: false,
          },
          playbackForwardMaxRateDecodeIFrame: 1,
          isWebrtcForOthers: true,
          demuxUseWorker: true,
          supportHls265: true,
        });
        let that = this;
        this.easyPlayer.on("error", function (type, msg) {
          that.closeVideo(true);
          that.$modal.msgError(this.videoTitle + "视频加载出错");
        });
        this.autoplay && this.playVideo();
      }
    },

    playVideo() {
      this.easyPlayer.play(this.videoUrl).catch(() => {
        this.closeVideo();
        this.$modal.msgError(this.videoTitle + "视频加载出错");
      });
    },

    isLive() {
      const ext = [".mp4"];
      return !ext.some((v) => (this.videoUrl + "").includes(v));
    },
    closeVideo(isDestroy) {
      if (!this.easyPlayer) {
        return;
      }
      if(isDestroy){
        this.easyPlayer.destroy();
        this.easyPlayer = null;
        this.showPlayer = false;
      }else{
        this.$emit("closeVideo", this.index);
      }
    },
  },
};
</script>

<style lang="scss" scoped>
.video-box {
  position: relative;
  width: 100%;
  height: 100%;
  background: #000;
  overflow: hidden;
  z-index: 2;
  &.show-head {
    padding-top: 30px;
  }
  .video-head {
    position: absolute;
    left: 0;
    top: 0;
    width: 100%;
    height: 30px;
    display: flex;
    align-items: center;
    background: #333;
    padding: 0 10px;
    z-index: 5;
    .video-custom-title {
      color: #fff;
      font-size: 14px;
      width: 90%;
      overflow: hidden;
      text-overflow: ellipsis;
      white-space: nowrap;
    }
    .video-close {
      position: absolute;
      right: 15px;
      top: 50%;
      transform: translateY(-50%);
      width: 20px;
      height: 20px;
      cursor: pointer;
      background: url(./icons/icon-close.png) no-repeat center/auto 100%;
    }
  }

  .video-player-container {
    height: 100%;
    .video-player {
      position: relative;
      height: 100%;
      video {
        position: absolute;
        width: 100%;
        left: 0;
        top: 0;
        border: none;
      }
    }
    :deep(.easyplayer-controls-code-wrap) {
      display: none !important;
    }
  }
  .icon-play-box {
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    width: 160px;
    height: 50%;
    max-height: 160px;
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    .icon-play {
      display: inline-block;
      width: 50%;
      height: 50%;
      max-width: 100px;
      max-height: 100px;
      background: url(./icons/icon-play-logo.png) no-repeat center/auto 100%;
    }
    .icon-text {
      font-size: 14px;
      color: #fff;
      font-weight: bold;
      margin-top: 10px;
    }
  }
}
</style>
